package topo.autosearch;

import com.zhtelecom.common.snmp.SNMPTarget;
import com.zhtelecom.common.snmp.mib.MibIfEntry;
import com.zhtelecom.common.topograph.TopoDataSource;
import com.zhtelecom.common.topograph.TopoNode;
import com.zhtelecom.nms.framework.discover.DeviceInfo;
import com.zhtelecom.nms.framework.discover.DeviceTypeInfo;
import com.zhtelecom.nms.framework.discover.LinkInfo;
import com.zhtelecom.nms.framework.discover.PortInfo;
import java.util.List;
import java.util.Map;
import topo.MainFrame;
import topo.data.NodeDevice;
import topo.data.NodeLink;
import topo.data.NodeType;
import topo.service.TopoDataManager;

public class InitTopo {
    static final int X_NUM = 10;
    static final int Local_X_W = 75;
    static final int Local_y_H = 100;

    public static void initTopoNode(List<DeviceInfo> deviceInfoList) {
        int i = 0;
        for (DeviceInfo deviceInfo : deviceInfoList) {
            if (NodeType.nodeTypeMap.containsKey(deviceInfo.getDeviceType().getDeviceProductType())) {
                NodeDevice node = deviceToNode(deviceInfo);
                TopoNode topoNode = new TopoNode(node);
                String deviceType = node.getNodeType();
                NodeType nodeType = NodeType.getNodeTypeByKey(deviceType);
                if (nodeType != null) {
                    topoNode.setImageIcon(nodeType.getTypeImage());
                } else {
                    topoNode.setImageIcon(TopoNode.defaultIcon);
                }

                topoNode.setDisplayName(deviceInfo.getDeviceIP());
                int nodeSEQ = i + 1;
                int x = nodeSEQ % 10;
                if (x == 0) {
                    x = 10;
                }
                int poseX = (x - 1) * 75 + 30;

                int y = nodeSEQ / 10;
                if (nodeSEQ % 10 != 0) {
                    y++;
                }
                int poseY = (y - 1) * 100 + 30;
                topoNode.setX(poseX);
                topoNode.setY(poseY);

                i++;

                MainFrame.source.addTopoData(topoNode);
            }
        }
    }

    public static void initTopoLink(List<LinkInfo> linkInfoList) {
        MainFrame.source.beginChange();
        for (LinkInfo linkInfo : linkInfoList) {
            if (linkInfo.linkType.equals(LinkInfo.LinkTypeInbound)) {
                NodeLink nodeLink = linkToNodeLink(linkInfo);
                TopoDataManager.getInstance().addLink(nodeLink);
            }
        }
        MainFrame.source.commitChange();
    }

    private static NodeDevice deviceToNode(DeviceInfo deviceInfo) {
        NodeDevice node = new NodeDevice();
        node.setDeviceLogicType(deviceInfo.getDeviceType().getLogicType());
        node.setDeviceMainMAC(deviceInfo.getDeviceMAC());
        node.setDeviceSystemDesc(deviceInfo.getDeviceDesc());
        node.setDeviceSystemOID(deviceInfo.getDeviceSysOID());
        node.setDisplayName(deviceInfo.getDeviceName());
        node.setIpAddress(deviceInfo.getDeviceIP());

        if (deviceInfo.getSnmpTarget() != null) {
            node.setManagerProtocol("SNMP");
            node.setSnmpPort(deviceInfo.getSnmpTarget().port);
            node.setSnmpVersion(deviceInfo.getSnmpTarget().snmpVersion);
            node.setReadCommit(deviceInfo.getSnmpTarget().readCommunity);
            node.setWriteCommit(deviceInfo.getSnmpTarget().writeCommunity);
        } else {
            node.setManagerProtocol("非SNMP");
        }
        node.setNodeDesc(deviceInfo.getDeviceDesc());
        node.setNodeType(deviceInfo.getDeviceType().getDeviceProductType());
        node.setNodeName(node.genKey());

        return node;
    }

    private static NodeLink linkToNodeLink(LinkInfo link) {
        NodeLink nodeLink = new NodeLink();
        NodeDevice fromNode = deviceToNode(link.startDevice);
        NodeDevice endNode = deviceToNode(link.endDevice);
        nodeLink.setFromNode(fromNode.getNodeName());
        nodeLink.setFromNodeIfIndex(link.startPort);
        nodeLink.setFromPhysicalPort(link.startPort);

        PortInfo startPortInfo = link.startPortInfo;
        PortInfo endPortInfo = link.endPortInfo;
        long bandWidth = 0L;

        MibIfEntry startMibIf = startPortInfo.getPortMibIfEntry();
        if (startMibIf != null) {
            if (startPortInfo.isUseIfIndexPort) {
                int startIndex = startMibIf.getIfIndex();
                nodeLink.setFromNodeIfIndex(startIndex);
            } else {
                nodeLink.setFromNodeIfIndex(link.startPort);
            }
            String startName = startMibIf.getIfDescr();
            nodeLink.setFromNodeIfName(startName);
            bandWidth = startMibIf.getIfOutOctets();
        } else {
            nodeLink.setFromNodeIfIndex(link.startPort);
        }
        MibIfEntry mibIf = endPortInfo.getPortMibIfEntry();
        if (mibIf != null) {
            if (endPortInfo.isUseIfIndexPort) {
                int endIndex = mibIf.getIfIndex();
                nodeLink.setToNodeIfIndex(endIndex);
            } else {
                nodeLink.setToNodeIfIndex(link.endPort);
            }
            String endName = mibIf.getIfDescr();
            nodeLink.setToNodeIfName(endName);
            if (nodeLink.getLinkBandwidth() <= 0L) {
                bandWidth = mibIf.getIfSpeed();
            }

        } else {
            nodeLink.setToNodeIfIndex(link.endPort);
        }
        bandWidth /= 1000000L;
        nodeLink.setLinkBandwidth(bandWidth);
        nodeLink.setLinkType(link.linkType);
        nodeLink.setToNodeIfIndex(link.endPort);
        nodeLink.setToNode(endNode.getNodeName());
        nodeLink.setToPhysicalPort(link.endPort);
        nodeLink.setLinkName(nodeLink.genKey());

        return nodeLink;
    }
}